Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tech debt: update ledger dependencies #346

Closed
wants to merge 1 commit into from

Conversation

0xMillz
Copy link
Contributor

@0xMillz 0xMillz commented Oct 14, 2021

Also removes u2f which Ledger has deprecated

@vercel
Copy link

vercel bot commented Oct 14, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/shapeshift/hdwallet/5vJwgoA5rtMuU1bCqexW28VGfk9P
✅ Preview: https://hdwallet-git-updateledgerdependencies-shapeshift.vercel.app

[Deployment for 599f164 failed]

@mrnerdhair mrnerdhair self-requested a review October 14, 2021 23:48
@@ -9,16 +9,19 @@
"dev": "yarn parcel index.html",
"clean": "rm -rf dist .cache"
},
"engines": {
"node": "14.x"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is accurate. You're 100% certain this doesn't run on older versions of node? I'm running node 12.x and it compiles fine for me.

@@ -13,6 +13,9 @@
"clean": "rm -rf dist tsconfig.tsbuildinfo",
"prepublishOnly": "yarn clean && yarn build"
},
"engines": {
"node": "14.x"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is wrong because we use hdwallet in axiom which runs node 12. "engines" will prevent use of any other version of node. If you absolutely want to add "engines" then use ">= 12" or whatever the minimum version of node is that is compatible.

Copy link
Contributor

@cjthompson cjthompson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove or change the engines property in package.json.

Comment on lines +37 to +39
"engines": {
"node": "14.x"
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We removed engine restrictions on purpose several versions ago. If a dependency requires at least node 14, then it's own engines block should be set; if it isn't, we should set it on the affected hdwallet-* package, but it shouldn't be all-pervasive, and should at least specify >=14 instead of 14.x.

packages/hdwallet-ledger-webusb/package.json Show resolved Hide resolved
@@ -10,19 +10,20 @@ import * as ledger from "@shapeshiftoss/hdwallet-ledger";

const RECORD_CONFORMANCE_MOCKS = false;

function translateCoin(coin: string): { new (transport: Transport): Record<string, (...args: any[]) => unknown> } {
return core.mustBeDefined(({
function translateCoin(coin: string): any | unknown {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any | unknown is just any, which probably isn't what's intended, and if it is it probably shouldn't be.

Suggested change
function translateCoin(coin: string): any | unknown {
function translateCoin(coin: string): äny! äny! Cthulhu fhtagn! {

packages/hdwallet-ledger/package.json Show resolved Hide resolved
Comment on lines +55 to +60
return {
[core.BTCInputScriptType.SpendAddress]: "legacy",
[core.BTCInputScriptType.CashAddr]: "legacy",
[core.BTCInputScriptType.SpendWitness]: "bech32",
[core.BTCInputScriptType.SpendP2SHWitness]: "p2sh",
} as Partial<Record<core.BTCInputScriptType, string>>)[scriptType]);
}[scriptType];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leaves no runtime check that all core.BTCInputScriptType options are accounted for. A switch statement without a default case would instead provide a static guarantee of full coverage -- though it would still need a default throw for forwards-compatibility. I'd argue it's too verbose, and a simple map and throw-if-undefined (a la core.mustBeDefined) is preferable.

Suggested change
return {
[core.BTCInputScriptType.SpendAddress]: "legacy",
[core.BTCInputScriptType.CashAddr]: "legacy",
[core.BTCInputScriptType.SpendWitness]: "bech32",
[core.BTCInputScriptType.SpendP2SHWitness]: "p2sh",
} as Partial<Record<core.BTCInputScriptType, string>>)[scriptType]);
}[scriptType];
switch (scriptType) {
case core.BTCInputScriptType.SpendAddress:
return "legacy";
case core.BTCInputScriptType.CashAddr:
return "legacy";
case core.BTCInputScriptType.SpendWitness:
return "bech32";
case core.BTCInputScriptType.SpendP2SHWitness:
return "p2sh";
}

Comment on lines +195 to +197
const keySet = msg.inputs[i].addressNList
.map((num) => (num >= core.HARDENED ? `${num - core.HARDENED}'` : num))
.join("/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const keySet = msg.inputs[i].addressNList
.map((num) => (num >= core.HARDENED ? `${num - core.HARDENED}'` : num))
.join("/")
const keySet = core.addressNListToBIP32(msg.inputs[i].addressNList).replace(/^m\//, "")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I originally coded but either the hot-reload failed to pick up my changes or I screwed up the regex and it wasn't working. I'll see if I can pull it off this time.

@@ -58,7 +58,7 @@ export function arrayify(value: string): Uint8Array {
return new Uint8Array(result);
}

const HARDENED = 0x80000000;
export const HARDENED = 0x80000000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this would need a better name for export at this level; also it doesn't need to be exported if if you use core.addressNListToBIP32 as shown below.

Suggested change
export const HARDENED = 0x80000000;
const HARDENED = 0x80000000;

Comment on lines 359 to +367

const appName = _.get(networksUtil[core.mustBeDefined(core.slip44ByCoin(coin))], "appName");
if (!appName) {
throw new Error(`Unable to find associated app name for coin: ${coin}`);
}

const res = await this.transport.call(null, "getAppAndVersion");
handleError(res, this.transport);

const {
payload: { name: currentApp },
} = res;
if (currentApp !== appName) {
throw new core.WrongApp("Ledger", appName);
if (currentApp !== coin) {
throw new core.WrongApp("Ledger", coin);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ledger app names do not equal hdwallet-core's Coin names. Note for example BitcoinCash vs Bitcoin Cash and Digibyte vs DigiByte. I don't think you can avoid a lookup of some kind here.

return core.mustBeDefined(({
function translateMethod(method: string): (transport: Transport, ...args: any[]) => any {
return ({
decorateAppAPIMethods: TransportWebUSB.decorateAppAPIMethods,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a static method -- does this work at runtime?

@@ -14,7 +14,7 @@ import {
handleError,
} from "./utils";

const supportedCoins = ["Testnet", "Bitcoin", "BitcoinCash", "Litecoin", "Dash", "DigiByte", "Dogecoin"];
export const supportedCoins = ["Testnet", "Bitcoin", "BitcoinCash", "Litecoin", "Dash", "DigiByte", "Dogecoin"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This export is not needed since btcSupportsCoin is already available.

@mrnerdhair
Copy link
Contributor

(whoops, wrong branch!)

@mrnerdhair
Copy link
Contributor

@millsmcilroy I did some cleanup on this and added the TS magic needed to pull in the types from the ledger packages. It needed a couple of supporting PRs (#349, #350) to enable strict mode first; since we use null to switch to a specific set of methods in call(null, "foo"), strictNullChecks needs to be turned on so that null isn't treated as a member of every type.

I opened #351 separately to avoid clobbering your branch history with the extra strict-mode stuff, but if you like it IMO it'd make more sense to force-push it to this branch and use this PR instead of that one.

@0xMillz
Copy link
Contributor Author

0xMillz commented Oct 18, 2021

Closing in favor of #351

@0xMillz 0xMillz closed this Oct 18, 2021
@0xMillz 0xMillz deleted the update_ledger_dependencies branch October 18, 2021 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants